export function objToString(obj) {
  for (let i in obj) {
    console.log(`${i}=${obj[i]}`)
  }
}

// 短信验证码验证
export function checkSMS(nums) {
  const reg = /^\d{6}$/
  return nums.length !== 6 && !reg.test(nums)
    ? [false, '请输入6位短信验证码']
    : [true, '']
}
// 手机号码验证
export function checkMobile(mobile) {
  var myreg = /^(((13[0-9]{1})|(14[5,7]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/
  if (!myreg.test(mobile)) {
    return [false, '请输入有效的手机号码！']
  } else {
    localStorage.mobile_number = mobile
    return [true, '']
  }
}

// 身份证验证
export function isCardNo(card) {
  // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X
  var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
  return reg.test(card) === false
    ? [false, '请输入正确的身份证号码']
    : [true, '']
}

// 验证预约时间，必须提前一天，并且不能为周末
function checkReservationDate(time) {
  const t = new Date(time)
  const nt = new Date()
  nt.setHours(0)
  nt.setMinutes(0)
  nt.setSeconds(0)
  if (t.getDay() === 6 || t.getDay() === 7) {
    return [false, '周末无法预约，请预约工作日办理']
  }
  if (parseInt((t - nt) / 1000 / (24 * 3600)) <= 0) {
    return [false, '请提前一天进行预约！']
  }
  return [true, '']
}

// 预约验证
export function checkReservation(obj) {
  const ver_mob = checkMobile(obj.phone_number)
  const ver_card = isCardNo(obj.id_no)
  const ver_date = obj.booking_time
    ? checkReservationDate(obj.booking_time)
    : [false, '预约时间不能为空']

  if (!ver_date[0]) return ver_date
  if (obj.booking_type === 2) {
    if (!obj.address) return [false, '家庭地址不能为空！']
  }
  if (!obj.username) return [false, '申请人姓名不能为空！']
  if (!ver_card[0]) return ver_card
  if (!ver_mob[0]) return ver_mob

  return [true, '']
}

// 转码
export function urlEncode(param, key, encode) {
  if (param == null) return ''
  var paramStr = ''
  var t = typeof param
  if (t === 'string' || t === 'number' || t === 'boolean') {
    paramStr +=
      '&' +
      key +
      '=' +
      (encode == null || encode ? encodeURIComponent(param) : param)
  } else {
    for (var i in param) {
      var k =
        key == null
          ? i
          : key + (param instanceof Array ? '[' + i + ']' : '.' + i)
      paramStr += urlEncode(param[i], k, encode)
    }
  }
  return paramStr
}

// 距离排序
export function getDistanceSortList(arr) {
  return arr.sort((a, b) => {
    return a.distance - b.distance
  })
}

// 距离比较
export function getDistance(lat1, lng1, lat2, lng2) {
  var radLat1 = lat1 * Math.PI / 180.0
  var radLat2 = lat2 * Math.PI / 180.0
  var a = radLat1 - radLat2
  var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0
  var s =
    2 *
    Math.asin(
      Math.sqrt(
        Math.pow(Math.sin(a / 2), 2) +
          Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)
      )
    )
  s = s * 6378.137 // EARTH_RADIUS;
  s = Math.round(s * 10000) / 10000
  return s
}

// 地图设置
export function TMap(key) {
  return new Promise(function(resolve, reject) {
    window.init = function() {
      resolve(window.qq) // 注意这里
    }
    var script = document.createElement('script')
    script.type = 'text/javascript'
    script.src =
      'https://map.qq.com/api/js?v=2.exp&libraries=geometry&callback=init&key=' +
      key
    script.onerror = reject
    document.head.appendChild(script)
  })
}

// 获取位置
export function getLoacation() {
  var geolocation = new this.qq.maps.Geolocation(
    'OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77',
    'lvpin100'
  )
  var options = {
    timeout: 2000
  }
  geolocation.getLocation(postion, err, options)

  function postion(postion) {
    // next([true, postion]);
    window.userLocation = [true, postion]
  }

  function err() {
    // next([false, "定位失败"]);
    window.userLocation = [false, '定位失败']
  }
}

// 延时
export function delay(time, random) {
  const t = time + Math.floor(Math.random() * random)
  return new Promise(resolve => {
    setTimeout(resolve, t)
  })
}
